package cn.daniel.goods.controller;


import cn.daniel.commonutils.Response;
import cn.daniel.goods.entity.GoodsImage;
import cn.daniel.goods.service.GoodsImageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Daniel_li
 * @since 2020-12-29
 */
@RestController
@RequestMapping("/service/goods/image")
public class GoodsImageController {

    @Autowired
    private GoodsImageService goodsImageService;

    /**
     * 添加物品的一个照片
     * @param goodsImage
     * @return
     */
    @PostMapping("/addGoodsImage")
    public Response addGoodsImage(@RequestBody GoodsImage goodsImage){
        boolean save = goodsImageService.save(goodsImage);
        return save?Response.ok():Response.error();
    }

    /**
     * 通过goodsId获取对应物品的相册
     *
     * @param goosId
     * @return
     */
    @GetMapping("/getGoodsImageList/{goosId}")
    public Response getGoodsImageList(@PathVariable String goosId){
        QueryWrapper<GoodsImage> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id",goosId);
        wrapper.isNull("comment_id");
        List<GoodsImage> list = goodsImageService.list(wrapper);
        return Response.ok().data("goodsImageList",list);
    }

    /**
     * 根据照片id更新物品对应的照片
     * @param goodsImage
     * @return
     */
    @PostMapping("/updateGoodsImageById")
    public Response updateGoodsImageById(@RequestBody GoodsImage goodsImage){
        boolean b = goodsImageService.updateById(goodsImage);
        return b ? Response.ok():Response.error();
    }

    /**
     * 根据照片id删除对应的照片
     * @param id
     * @return
     */
    @DeleteMapping("/removeGoodsImageById/{id}")
    public Response removeGoodsImageById(@PathVariable String id){
        boolean b = goodsImageService.removeById(id);
        return b?Response.ok():Response.error();
    }

    /**
     * 批量添加或更新物品相册
     * @param goodsImageList
     * @return
     */
    @ApiOperation("批量添加或更新物品相册")
    @PostMapping("/addOrUpDateGoodsImage")
    public Response addOrUpDateGoodsImage(@RequestBody List<GoodsImage> goodsImageList){
        boolean success=goodsImageService.addOrUpDateGoodsImage(goodsImageList);
        return success?Response.ok():Response.error();

    }



}

